package no.hig.imt4721.presentation;

import java.text.DecimalFormat;
import java.util.List;

import javax.swing.JFrame;

import no.hig.imt4721.model.DataPoint;
import no.hig.imt4721.tests.Test;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class DETGraph extends JFrame
{
	private static final long serialVersionUID = 6859382038761992009L;
	
	private static String PLOT_TITLE = "DET Curves";
	private static String X_AXIS_LABEL = "FNMR";
	private static String Y_AXIS_LABEL = "FMR";
	
	public void draw(List<Test> tests)
	{
		XYSeriesCollection dataset = new XYSeriesCollection();
		
		for(Test test : tests)
		{
			DecimalFormat formatter = new DecimalFormat("00.00");
			
			XYSeries series = new XYSeries(test.getName() + " (EER: " + formatter.format(test.getEER() * 100) + "%)");
			
			List<DataPoint> dataPoints = test.getDataPoints();
			
			if(dataPoints != null)
			{
				for(int i = 0; i < dataPoints.size(); i++)
					series.add(dataPoints.get(i).getFNMR(), dataPoints.get(i).getFMR());
			
        		dataset.addSeries(series);
			}
		}
        
        JFreeChart chart = ChartFactory.createXYLineChart(PLOT_TITLE, X_AXIS_LABEL, Y_AXIS_LABEL, dataset, PlotOrientation.VERTICAL, true, false, false);
        
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(800, 600));
        setContentPane(chartPanel);
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle(PLOT_TITLE);
        
		pack();
		setVisible(true);
	}
}
